package Model
{
	import Controller.AreasController;
	
	import flash.events.Event;
	
	import mx.events.PropertyChangeEvent;

	public class HeatedSpaceVolume extends SimpleComponent
	{
		private var _area:Number;
		private var _height:Number;
		private var _volume:Number;
		private var areaController:AreasController;
		private var propertyChangedEvent:PropertyChangeEvent = new PropertyChangeEvent("propertyChanged");
		public static var counter:Number = 1;
		
		public function HeatedSpaceVolume(description:String="", quantity:Number=1, area:Number = 0, height:Number = 0, volume:Number = 0)
		{
			this.areaController = new AreasController();
			
			super(description, quantity);
			this.id = generateId();
			this._area = area;
			this._height = height;
			this._volume = volume;
			this.addEventListener("internalChange", handleInternalChange);
		}
		
		override public function generateId():String
		{
			var result:String = "";
			
			if(counter < 9)
			{	
				result = "HV0" + counter.toString();
			}
			else
			{
				result = "HV" + counter.toString();
			}
			
			counter++;
			
			return result;
		}

		public function get area():Number
		{
			return _area;
		}

		public function set area(value:Number):void
		{
			_area = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get height():Number
		{
			return _height;
		}

		public function set height(value:Number):void
		{
			_height = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get volume():Number
		{
			return _volume;
		}

		public function set volume(value:Number):void
		{
			_volume = value;
			this.propertyChangedEvent.property = this.volume;
			this.dispatchEvent(this.propertyChangedEvent);
		}
		
		public function calculateHeatedSpaceVolume():void
		{
			this.volume = this.quantity *(this.area * this.height);
		}
		
		protected function handleInternalChange(event:Event):void
		{
			this.calculateHeatedSpaceVolume();	
			this.areaController.updateAreaOverviewStatus();
		}


	}
}